import { ImplementsInterface } from "../../../Decorator/Decorator";
import { ISingletonUpdate } from "../../Singleton/ISingletonUpdate";
import { Singleton } from "../../Singleton/Singleton";

@ImplementsInterface(ISingletonUpdate)
export class TimeInfo extends Singleton<TimeInfo> implements ISingletonUpdate {

    private static _Instance: TimeInfo;
    static get Instance() {
        return this._Instance;
    }

    protected set Instance(value: TimeInfo) {
        TimeInfo._Instance = value;
    }

    private _ServerMinusClientTime: number = 0;
    public get ServerMinusClientTime(): number {
        return this._ServerMinusClientTime;
    }
    public set ServerMinusClientTime(value: number) {
        this._ServerMinusClientTime = value;
    }

    private readonly dt1970 = new Date(0);

    private dt = new Date(0);

    public FrameTime!: number;

    constructor() {
        super();
        this.FrameTime = this.ClientNow();
    }

    public Update() {
        this.FrameTime = this.ClientNow();
    }

    public ToDateTime(timeStamp: number) {
        return this.dt.setTime(this.dt.getTime() + timeStamp);
    }

    public ClientNow() {
        return (Date.now() - this.dt1970.getTime());
    }

    public ServerNow() {
        return this.ClientNow() + TimeInfo.Instance.ServerMinusClientTime;
    }

    public ClientFrameTime() {
        return this.FrameTime;
    }

    public ServerFrameTime(): number {
        return this.FrameTime + TimeInfo.Instance.ServerMinusClientTime;
    }

    public Transition(d: Date) {
        return (d.getTime() - this.dt.getTime());
    }

    public Dispose(): void {

    }

}